package com.gitee.wsl.struct.grammar.ast.visitor

import com.gitee.wsl.struct.grammar.ast.node.ASTNode
import com.gitee.wsl.struct.grammar.ast.node.CompositeNode
import com.gitee.wsl.struct.grammar.ast.node.LeafNode


fun interface Visitor {

    fun visitNode(node: ASTNode){
        when(node){
            is CompositeNode ->{
                for (child in node.children) {
                    visitNode(child)
                }
            }
            is LeafNode -> visitLeaf(node)
            else -> {}
        }
    }

    fun visitLeaf(node: LeafNode)

    /* Stops the traversal when `true` is returned. The method is called immediately
   * after descending into a node. After stopping, there will be no
   * further calls to [Visitor.visit], including for this or any of the nodes on
   * the current path as it is
   * unwound.
   *
   * The default implementation does not stop until the walk is complete.
   */
    fun stopAt(node: ASTNode): Boolean = false

    /**
     * Skips traversing a node's children and subtree when `true`.
     *
     * The default implementation does not skip any nodes.
     */
    fun skipBelow(node: ASTNode): Boolean = false
}

